
import 'dart:convert';
// import 'intl';

class SaveInfo {
  String name;
  String stepIndex;
  double time;
  double totalCost;
  double totalReward;

  // String get formatedTime {
  //   // 将秒时间戳转换为 DateTime 对象
  //   DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(timestamp * 1000);
  //
  //   // 使用 intl 包中的 DateFormat 格式化日期和时间
  //   return DateFormat('yy-MM-dd HH:mm:ss').format(dateTime);
  // }

  SaveInfo({
    required this.name,
    required this.stepIndex,
    required this.time,
    required this.totalCost,
    required this.totalReward,
  });

  // 从 JSON 字符串构造 SaveInfo 对象
  factory SaveInfo.from(String json) {
    final Map<String, dynamic> data = jsonDecode(json);
    return SaveInfo(
      name: data['name'],
      stepIndex: data['step_index'],
      time: data['time'],
      totalCost: data['total_cost'],
      totalReward: data['total_reward'].toDouble(),
    );
  }

  // 从 JSON 字符串构造 SaveInfo 对象
  factory SaveInfo.fromMap(Map<String, dynamic> data) {
    return SaveInfo(
      name: data['name'],
      stepIndex: data['step_index'],
      time: data['time'],
      totalCost: data['total_cost'],
      totalReward: data['total_reward'].toDouble(),
    );
  }

  // 从 JSON 字符串列表批量构造 SaveInfo 对象列表
  static List<SaveInfo> fromJsonList(String jsonList) {
    final List<dynamic> dataList = jsonDecode(jsonList);
    return dataList.map((item) => SaveInfo.fromMap(item)).toList();
  }

  @override
  String toString() {
    return 'SaveInfo(name: $name, stepIndex: $stepIndex, time: $time, totalCost: $totalCost, totalReward: $totalReward)';
  }
}


void main() {
  // String json = r'{"name": "test", "step_index": 2, "time": "1744305449.1957333", "total_cost": 2, "total_reward": 0}';
  //
  // SaveInfo saveInfo = SaveInfo.from(json);
  // print(saveInfo);

  String jsonList = r'''[
    {"name":"test","step_index":"2/30","time":1744305449.1957333,"total_cost":2.00,"total_reward":0.00},
    {"name":"test689","step_index":"1/30","time":1744306080.9256325,"total_cost":1.00,"total_reward":0.00}
  ]''';

  List<SaveInfo> saveInfos = SaveInfo.fromJsonList(jsonList);
  for (var saveInfo in saveInfos) {
    print(saveInfo);
  }

}